package com.tools;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

@SuppressLint("SimpleDateFormat")
public class DailyyYogaTools {

	public static final String downloadURL = "https://market.android.com/details?id=";

	public static String getFormatDate(int year, int month, int day) {

		String _month = "";

		if (month > 9) {
			_month = ("" + month);
		} else {
			_month = ("0" + month);
		}

		String _day = "";
		if (day > 9) {
			_day = ("" + day);
		} else {
			_day = ("0" + day);
		}

		return year + "-" + _month + "-" + _day;

	}

	public static Date getFormatTime(String formatDate) throws ParseException {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.parse(formatDate);

	}

	public static String getSystemCurrentTime() {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(new Date());
	}

	public static long format2millis(String formatTime) throws ParseException {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.parse(formatTime).getTime();
	}

	public static String millis2format(long millis) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.format(new Date(millis));
	}

	@SuppressWarnings("deprecation")
	public static String formatDateAddformatDate(String formatDate1,
			String formatHm) throws ParseException {

		Log.d("formatDateAddformatDate", "formatDate1=" + formatDate1);
		Log.d("formatDateAddformatDate", "formatHm=" + formatHm);

		Calendar calendar = Calendar.getInstance();

		Date date1 = getFormatTime(formatDate1);

		calendar.setTimeInMillis(date1.getTime());

		Date date2 = getFormatTime("2013-12-02 " + formatHm);

		calendar.add(Calendar.HOUR, date2.getHours());

		calendar.add(Calendar.MINUTE, date2.getMinutes());

		String result = new SimpleDateFormat("yyyy-MM-dd HH:mm")
				.format(calendar.getTime());
		Log.d("formatDateAddformatDate", "result=" + result);

		return result;

	}

	@SuppressWarnings("deprecation")
	public static int[] date2HM(String date) throws ParseException {

		int[] hm = new int[2];

		Date date2 = getFormatTime(date);

		hm[0] = date2.getHours();
		hm[1] = date2.getMinutes();

		return hm;

	}

	public static boolean pkgIsInstall(String inPkg, Context inContext) {
		try {

			inContext.createPackageContext(inPkg,
					Context.CONTEXT_IGNORE_SECURITY
							| Context.CONTEXT_INCLUDE_CODE);
		} catch (NameNotFoundException e) {
			return false;
		}

		return true;
	}

	public static Context getOtherContext(Context inContext, String inPkg) {

		try {
			return inContext.createPackageContext(inPkg,
					Context.CONTEXT_IGNORE_SECURITY
							| Context.CONTEXT_INCLUDE_CODE);
		} catch (NameNotFoundException e) {
			return null;
		}

	}

	public static Bitmap getPlugPrompt(Context inContext, String inPkg,
			String inPrompt) {

		try {
			Context plugContext = getOtherContext(inContext, inPkg);
			InputStream inputStream = plugContext.getAssets().open(inPrompt);
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
			return bitmap;
		} catch (Exception e) {

			return null;
		}

	}

	public static int getPlugVc(String plugName, Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo pi = packageManager.getPackageInfo(plugName, 0);
			return pi.versionCode;
		} catch (Exception e) {
		}
		return -1;
	}

	public static String getLangPath() {

		String lang = Locale.getDefault().toString();
		if (lang.equals("zh_CN")) {
			return "zh_CN_";
		}
		if (lang.equals("zh_TW")) {
			return "zh_TW_";
		}

		if (lang.equals("ja_JP")) {
			return "ja_JP_";
		}

		return "";
	}

	public static String getServerLangKey() {
		return "CN";
	}

	public static String getLang() {

		if (Locale.getDefault().toString().contains("zh")) {
			return "zh";
		} else {
			return "en";
		}

	}

	public static String getTimeOffset() {

		int offet = java.util.Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 1000 / 60 / 60;
		Log.d("timezone", "offet=" + offet + ">>" + getTimeZoneText());

		return getTimeZoneText();

	}

	private static String getTimeZoneText() {
		TimeZone tz = java.util.Calendar.getInstance().getTimeZone();
		boolean daylight = tz.inDaylightTime(new Date());
		StringBuilder sb = new StringBuilder();

		sb.append(formatOffset(tz.getRawOffset()
				+ (daylight ? tz.getDSTSavings() : 0)));

		return sb.toString();
	}

	private static String formatOffset(int off) {

		int hour = off / (1000 * 60 * 60);

		float m = (off % (1000 * 60 * 60)) / 60000;

		if (m < 0) {

			m *= -1;
		}
		Log.d("timezone", hour + " : " + m);

		int mm = (int) ((m / 60) * 100);

		return hour + "." + mm;
	}

}
